import 'dart:math';

import 'package:calculate/utils/CalcUtils.dart';
import 'package:calculate/widgets/CalcKeyBoard.dart';
import 'package:decimal/decimal.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/physics.dart';

class CalcPage extends StatefulWidget {
  @override
  _CalcPageState createState() => _CalcPageState();
}

class _CalcPageState extends State<CalcPage> {

  String _calculateExpress = '';
  List<String> _expressStack = List();

  Decimal _result = Decimal.zero;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getCalcAppBar(),
      body: Column(
        children: <Widget>[
          getScreenArea(),
          getKeyboardArea(),
        ],
      ),
    );
  }

  getCalcAppBar() {
    return AppBar(
      backgroundColor: Colors.transparent,
      elevation: 0,
      iconTheme: IconThemeData(color: Color(0xFF1A1A1A), size: 14),
      leading: GestureDetector(
        child: Icon(IconData(0xe60a, fontFamily: 'icon-font')),
      ),
      actions: <Widget>[
        GestureDetector(
          child: Padding(
            padding: EdgeInsets.all(15),
            child: Icon(IconData(0xe60b, fontFamily: 'icon-font')),
          ),
        )
      ],
      centerTitle: true,
      textTheme: TextTheme(
        headline4: TextStyle(color: Color(0xFF1A1A1A))
      ),
      title: RichText(
        text: TextSpan(
          text: 'Calc',
          style: TextStyle(color: Color(0xFF1A1A1A), fontSize: 20, fontWeight: FontWeight.w700),
          children: [
            TextSpan(text: '.', style: TextStyle(color: Color(0xFFFF3E39), fontSize: 20, fontWeight: FontWeight.w700))
          ]
        ),
      ),
    );
  }

  getScreenArea() {
    return Container(
      color: Color(0xffFAFAFA),
      height: 240,
      padding: EdgeInsets.symmetric(
        horizontal: 15,
        vertical: 20
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: <Widget>[
          getExpressArea(),
          getResultArea()
        ],
      ),
    );
  }

  getKeyboardArea() {
    return Expanded(
        child: Container(
          color: Colors.white,
          padding: EdgeInsets.symmetric(horizontal: 15, vertical: 30),
          child: CalcKeyboard(
            onchange: (value) {
              setState(() {
                var calcData = CalcUtils.appendStack(value, _expressStack, _calculateExpress);
                _calculateExpress = calcData.express;
                _expressStack = calcData.expressStack;
                _result = calcData.result;
                print(_calculateExpress);
              });
            },
            onSubmit: () {
              setState(() {
                _calculateExpress = _result.toString();
                _expressStack.clear();
                _expressStack.add(_result.toString());
              });
            },
          ),
        )
    );
  }

  getExpressArea() {
    return Container(
      child: Text(
        _calculateExpress,
        style: TextStyle(
            fontSize: 24,
            color: Color(0xff1A1A1A),
          letterSpacing: 2
        ),
      ),
    );
  }

  getResultArea() {
    return Container(
      alignment: Alignment.centerRight,
      margin: EdgeInsets.only(top: 10),
      child: Text(_result.toString(),
        maxLines: 1,
        style: TextStyle(
          fontSize: 40,
          color: Color(0xff1A1A1A),
          fontWeight: FontWeight.w600,
        ),
      ),
    );
  }

}
